package com.googlecode.slm.web.springweb;


import com.googlecode.slm.domain.web.UserNode;
import com.googlecode.slm.service.UserService;
import com.googlecode.slm.util.CONSTANTS;
import com.googlecode.slm.web.BasicControler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpSession;

/**
 * 用户登陆
 * 用户注册
 * User: liguangjian
 * Date: 11-10-8
 * Time: 下午7:44
 * To change this template use File | Settings | File Templates.
 */
@Controller
@SessionAttributes(CONSTANTS.SESSION_USER_LGON)
public class WelcomeControler extends BasicControler{
	
	/**
	 * 不进入认证
	 */
	public boolean isAttestation() {
		return false;
	}
	
    @Autowired
    @Qualifier(value = "userService")
    private UserService userService;

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    
    @RequestMapping(value = "/reg.form", method = RequestMethod.POST)
    public String reg(String name,
                      String password,
                      String verifypass,
                      String grade,
                      String email,
                      String code,
                      HttpSession session,
                      ModelMap model){
        //取出session
        String validateCode = (String)session.getAttribute(CONSTANTS.SESSION_VALIDATECODE);
        session.removeAttribute(CONSTANTS.SESSION_VALIDATECODE);
        UserNode node = new UserNode();
        node.setName(name);
        node.setPassword(password);
        node.setGrade(Integer.parseInt(grade));
        node.setEmail(email);
        if (validateCode.equals(code)){
        	
        }
        if (userService.reg(node)==false)
            return CONSTANTS.VIEWURL_ERROR;
        return CONSTANTS.VIEWURL_SUCCESS;
    }
    
    @RequestMapping(value = "/login.form", method = RequestMethod.POST)
    public String login(String name, String password, int grade, String code, ModelMap model, HttpSession session){
//    	String validateCode = (String)session.getAttribute(CONTEXT.SESSION_VALIDATECODE);
//        session.removeAttribute("validateCode");
//        if (!code.equals(validateCode)){
//            model.put("info", "误码不能正确");
//            return CONTEXT.VIEWURL_ERROR;
//        }
//        if (name==null||name.trim().equals("")){
//            model.put("info", "用户名不能为空");
//            return CONTEXT.VIEWURL_ERROR;
//        } if (password==null||password.trim().equals("")){
//            model.put("info", "密码不能为空");
//            return CONTEXT.VIEWURL_ERROR;
//        } if (grade==0){
//            model.put("info", "用户级别不能为空");
//            return CONTEXT.VIEWURL_ERROR;
//        } if (code==null||code.trim().equals("")){
//            model.put("info", "误码不能为空");
//            return CONTEXT.VIEWURL_ERROR;
//        }
        
        UserNode node = new UserNode();
        node.setName(name);
        node.setPassword(password);
        node.setGrade(grade);
        node = userService.login(node);
        if (node!=null){
        	model.put(CONSTANTS.SESSION_USER_LGON, node);
            return CONSTANTS.VIEWURL_WELCOME;
        } else {
            model.put("info", "登陆错误");
            return CONSTANTS.VIEWURL_ERROR;
        }
    }


}
